package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.ReadingMeterStatusBean;
import com.koron.css2.baseConfig.mapper.ReadingMeterStatusMapper;
import com.koron.css2.clientCenter.bean.MeterReadingListBean;
import com.koron.css2.clientCenter.bean.PageTotalBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserChangeQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.DictionaryVO;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户抄表信息查询列表
 * 
 * @author 甄玉晴
 *
 */
public class MeterReadingList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			MessageBean<PageTotalBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageTotalBean.class);
			UserChangeQueryBean bean = JsonUtils.objectToPojo(req.getData(), UserChangeQueryBean.class);
			UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<MeterReadingListBean> list = mapper.meterReadingList(bean);
			// 获取抄表状态
			ReadingMeterStatusMapper statusMapper = factory.getMapper(ReadingMeterStatusMapper.class);
			List<ReadingMeterStatusBean> statusList = statusMapper.readingMeterStatusList();

            //抄表标记
            List<DictionaryVO> mrf = Tools.listDicByCode(factory, "MRF");
            Map<String, String> mrfMap = mrf.stream().collect(Collectors.toMap(x -> x.getValue(), x -> x.getName()));

//			StaffMapper staffMapper = factory.getMapper(StaffMapper.class);
//			List<StaffBean> staffs = staffMapper.selectList(new StaffQueryBean());
			for (MeterReadingListBean read : list) {//循环遍历抄表状态
				for (ReadingMeterStatusBean status : statusList) {
					if (status.getStatusValue() == read.getStatus()) {
						read.setStatusName(status.getStatusName());
					}
				}
//				if(StringUtils.isNotBlank(read.getMeterReadingStaff())) {
//					for(StaffBean staff:staffs) {
//						if (staff.getCode() != null && staff.getCode().equals(read.getMeterReadingStaffAccount())) {
//							read.setMobile(staff.getMobile());
//						}
//					}
//				}
                read.setMeterReadFlagName(mrfMap.get(String.valueOf(read.getMeterReadFlag())));
			}
			Map<String,Object> total = mapper.meterReadingListTotal(bean);
			PageTotalBean returnVo = new PageTotalBean();
			returnVo.setDetail(new PageInfo<>(list));
			returnVo.setTotal(total);
			info.setData(returnVo);
			return info;
		} catch (Exception e) {
			logger.error("用户抄表信息查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
